package com.tanhua.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.api.MessageApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.enums.CommentType;
import com.tanhua.dubbo.pojo.Announcement;
import com.tanhua.dubbo.pojo.Comment;
import com.tanhua.dubbo.vo.PageInfo;
import com.tanhua.server.vo.AnnouncementVo;
import com.tanhua.server.vo.MessageCommentVo;
import com.tanhua.server.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.protocol.rest.support.ContentType;
import org.jdom2.Content;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.SimpleFormatter;

@Service
@Slf4j
public class MessageService {
    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;

    @DubboReference(version = "1.0.0")
    private MessageApi messageApi;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public PageResult queryLikesList(Integer page, Integer pageSize) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);

        //获取当前用户ID
        Long userId = UserThreadLocal.get();
        //根据当前用户ID查询对应点赞列表,返回pageinfo
        PageInfo pageInfo = this.messageApi.queryMessageList(userId, page, pageSize, CommentType.LIKE);

        ArrayList<MessageCommentVo> messageCommentVoList = this.queryMessageListUtil(pageInfo);
        pageResult.setItems(messageCommentVoList);
        return pageResult;
    }



    public PageResult queryCommentList(Integer page, Integer pageSize) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        
        //获取当前用户id
        Long userId = UserThreadLocal.get();
        //根据当前用户ID查询对应点赞列表,返回pageinfo
        PageInfo pageInfo = this.messageApi.queryMessageList(userId, page, pageSize, CommentType.COMMENT);

        ArrayList<MessageCommentVo> messageCommentVoList = this.queryMessageListUtil(pageInfo);
        pageResult.setItems(messageCommentVoList);
        
        return pageResult;
    }

    public PageResult queryLoveList(Integer page, Integer pageSize) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        
        //获取当前用户ID
        Long userId = UserThreadLocal.get();
        //根据当前用户ID查询对应点赞列表,返回pageinfo
        PageInfo pageInfo = this.messageApi.queryMessageList(userId, page, pageSize, CommentType.LOVE);

        ArrayList<MessageCommentVo> messageCommentVoList = this.queryMessageListUtil(pageInfo);
        pageResult.setItems(messageCommentVoList);
        return pageResult;
    }

    public ArrayList<MessageCommentVo> queryMessageListUtil(PageInfo pageInfo) {
        //查询到的数据
        List<Comment> commentList = pageInfo.getRecords();
        
        //搞个集合存放MessageCommentVo对象
        ArrayList<MessageCommentVo> messageCommentList = new ArrayList<>();

        for (Comment comment : commentList) {
            MessageCommentVo messageCommentVo = new MessageCommentVo();
            //获取id
            messageCommentVo.setId(ObjectUtil.toString(comment.getUserId()));
            //通过点赞的id获取头像,赋给LikeListVo集合的对象
            messageCommentVo.setAvatar(this.userInfoApi.queryByUserId(comment.getUserId()).getLogo());
            //通过点赞的id获取网名赋给likeListvo集合的对象
            messageCommentVo.setNickname(this.userInfoApi.queryByUserId(comment.getUserId()).getNickName());
            //通过点赞的id获取点赞时间,赋给likeListvo集合的对象
            messageCommentVo.setCreateDate(format.format(comment.getCreated()));
            //存放到集合中
            messageCommentList.add(messageCommentVo);
        }
        return messageCommentList;
    }
    public PageResult queryAnnouncementsList(Integer page, Integer pageSize) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        //调用messageApi中方法查询公告列表
        IPage<Announcement> announcementsList = this.messageApi.queryAnnouncementsList(page, pageSize);
        //创建集合存放vo对象
        ArrayList<AnnouncementVo> announcementVoList = new ArrayList<>();
        //获取查询后返回的数据列表
        List<Announcement> records = announcementsList.getRecords();
        //日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //遍历数据列表,Announcement存放到vo对象
        for (Announcement record : records) {
            AnnouncementVo announcementVo = new AnnouncementVo();
            announcementVo.setId(ObjectUtil.toString(record.getId()));
            announcementVo.setTitle(record.getTitle());
            announcementVo.setDescription(record.getDescription());

            try {
                announcementVo.setCreateDate(format.format(sdf.parse(record.getCreated())));
            } catch (Exception e) {
                e.printStackTrace();
            }
            //存放
            announcementVoList.add(announcementVo);
        }

        //数据存回pageResult
        pageResult.setItems(announcementVoList);

        return pageResult;
    }

    }

